import * as CryptoJS from 'crypto-js'
/**
 *加密处理
 */
export const encryption = (params) => {
	let {
		data,
		type,
		param,
		key
	} = params
	const result = JSON.parse(JSON.stringify(data))
	if (type === 'Base64') {
		param.forEach(ele => {
			result[ele] = btoa(result[ele])
		})
	} else {
		param.forEach(ele => {
			var data = result[ele]
			key = CryptoJS.enc.Latin1.parse(key)
			var iv = key
			// 加密
			var encrypted = CryptoJS.AES.encrypt(
				data,
				key, {
					iv: iv,
					mode: CryptoJS.mode.CBC,
					padding: CryptoJS.pad.ZeroPadding
				})
			result[ele] = encrypted.toString()
		})
	}
	return result
}

/**
 * @word 要加密的内容
 * @keyWord String  服务器随机返回的关键字
 *  */
export function aesEncrypt(word, keyWord = "XwKsGlMcdPMEhR1B") {
	var key = CryptoJS.enc.Utf8.parse(keyWord);
	var srcs = CryptoJS.enc.Utf8.parse(word);
	var encrypted = CryptoJS.AES.encrypt(srcs, key, {
		mode: CryptoJS.mode.ECB,
		padding: CryptoJS.pad.Pkcs7
	});
	return encrypted.toString();
}




// 保存图片
export const savePicture = (url) => {
	return new Promise((resolve, reject) => {
		uni.downloadFile({
			url: url,
			success: (e => {
				uni.saveImageToPhotosAlbum({
					filePath: e.tempFilePath,
					success(res) {
						resolve(res)
					},
					fail(e) {
						if (e.errMsg ===
							"saveImageToPhotosAlbum:fail:auth denied" ||
							e.errMsg ===
							"saveImageToPhotosAlbum:fail auth deny" ||
							e.errMsg ===
							"saveImageToPhotosAlbum:fail authorize no response"
						) {
							// 这边微信做过调整，必须要在按钮中触发，因此需要在弹框回调中进行调用
							uni.showModal({
								title: '提示',
								content: '需要您授权保存相册',
								showCancel: false,
								success: modalSuccess => {
									uni.openSetting({
										success(settingdata) {
											if (settingdata
												.authSetting[
													'scope.writePhotosAlbum'
													]) {
												uni.showModal({
													title: '提示',
													content: '获取权限成功,再次点击图片即可保存',
													showCancel: false,
												})
											} else {
												uni.showModal({
													title: '提示',
													content: '获取权限失败，将无法保存到相册哦~',
													showCancel: false,
												})
											}
										}
									})
								}
							})
						}
					}
				})
			})
		})
	})
}

	// 获取上个页面
export const getPrevPage = () => {
	let pages = getCurrentPages()
	let prev = pages[pages.length - 2]
	return prev
}

// 获取当前页面
export const getNowPage = () => {
	let pages = getCurrentPages()
	let current = pages[pages.length - 1]
	return current
}


